<template>
  <div style="height: calc(100vh);background-color:#fff;">
    <van-search
      v-model="value"
      placeholder="请输入搜索关键词"
      show-action
      @cancel="onCancel"
      @search="onSearch"
    />
    <ul>
      <li v-for="(item, index) in computedCinemaList" :key="index" class="cinema-list-item">
        <div class="cinema-info">
          <span class="cinema-name">{{ item.name }}</span>
          <span class="cinema-address">{{ item.address }}</span>
        </div>
        <div style="flex: 1;"></div>
        <div class="cinema-info">
          <span class="cinema-price"><span style="font-size: 10px;">￥</span>{{ item.lowPrice / 100 }}<span
            style="font-size: 10px;">起</span></span>
          <span class="cinema-distance"></span>
        </div>
        <div class="dividing-line"></div>
      </li>
    </ul>
  </div>
</template>

<script>
import Vue from 'vue'
import {Search, Toast} from 'vant'

Vue.use(Search)
export default {
  name: 'Search',
  data() {
    return {
      value: '',
    }
  },
  mounted() {
    if (this.$store.state.cinemaList.length === 0) {
      this.$store.dispatch('getCinemaList', this.$store.state.cityId)
    } else {
      console.log('缓存')
    }
  },
  computed: {
    computedCinemaList() {
      if (this.value === '') {return []}
      return this.$store.state.cinemaList.filter(
        item => item.name.toUpperCase().includes(this.value.toUpperCase()) || item.address.toUpperCase().includes(this.value.toUpperCase())
      )
    }
  },
  methods: {
    onSearch(val) {
      Toast(val)
    },
    onCancel() {
      this.$router.replace('/cinema')
    },
  },
}
</script>

<style lang="scss" scoped>
.cinema-list-item {
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  padding: 15px;
  position: relative;

  .cinema-info {
    display: flex;
    flex-direction: column;
    justify-content: center;
    max-width: 80%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;

    .cinema-address {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      font-size: 12px;
      margin-top: 5px;
      color: #797d82;
    }

    .cinema-price {
      color: #ff5f16;
    }
  }
}

</style>
